<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      label-width="100px"
    >
      <el-form-item label="示例值" prop="example">
        <el-input v-model="formData.example" />
      </el-form-item>
      <el-form-item label="高级特性" prop="advFeature">
        <el-input v-model="formData.advFeature" />
      </el-form-item>
      <el-form-item label="关联表名" prop="refTableId">
        <el-select v-model="formData.refTableId" clearable no-match-text="请选择X111X" no-data-text="请选择XX" placeholder="请选择" @clear="handleClearRefTable">
          <el-option
            v-for="(item, index) in refTables"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联数字字段" prop="refValueColumnId">
        <el-select v-model="formData.refValueColumnId" clearable placeholder="请选择">
          <el-option
            v-for="(item, index) in refColumns"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联显示字段" prop="refDescColumnId">
        <el-select v-model="formData.refDescColumnId" clearable placeholder="请选择">
          <el-option
            v-for="(item, index) in refColumns"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import * as CodegenApi from '@/api/infra/codegen'
import { PropType } from 'vue'

defineOptions({ name: 'InfraCodegenColumnAdvanceDialogForm' })

const props = defineProps({
  column: {
    type: Object as PropType<Nullable<CodegenApi.CodegenColumnVO>>,
    default: () => null
  }
})

const formRef = ref()
const formData = ref({
  example: null,
  refTableId: null,
  refValueColumnId: null,
  refDescColumnId: null,
  advFeature: null
})

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

/** 打开弹窗 */
const open = async (data) => {
  dialogVisible.value = true
  dialogTitle.value = "字段高级属性 " + data.columnName
  await loadReferenceTables()
  if (data.refTableId) await loadReferenceColumns(data.refTableId)
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

watch(
  () => props.column,
  async (column) => {
    if (!column) return
    formData.value = column as any
  },
  {
    deep: true,
    immediate: true
  }
)

/**
 * 监视参考表的变化，重新获取参考字段列表
 */
watch(
  () => props.column?.refTableId,
  async (newValue, oldValue) => {
    console.log('newValue', newValue, 'oldValue=', oldValue)
    if (newValue) await loadReferenceColumns(newValue)
  }
)

const refTables = ref([]) // 表定义列表
const refColumns = ref([])

const handleClearRefTable = () => {
  formData.value.refTableId = 0
  formData.value.refValueColumnId = 0
  formData.value.refDescColumnId = 0
  console.log('handleClearRefTable', formData.value)
}

const loadReferenceTables = async () => {
  try {
    // 加载表列表
    //tables.value = await CodegenApi.getCodegenTableList(0)
    const list = [{ label: '请选择', value: 0 }]
    const resData = await CodegenApi.getCodegenTableList(0)
    resData.forEach(item => {
      list.push({label: item.tableName + '：' + item.tableComment, value: item.id})
    })
    refTables.value = list
    console.log('refTables.value', refTables.value)
  } catch {}
}

const loadReferenceColumns = async (tableId) => {
  try {
    // 加载字段列表
    const list = [{ label: '请选择', value: 0 }]
    const resData = await CodegenApi.getColumnList(tableId)
    resData.forEach(item => {
      list.push({label: item.columnName + '：' + item.columnComment, value: item.id})
    })
    refColumns.value = list
    console.log('refColumns.value', refColumns.value)
  } catch {}
}
</script>
